package com.arvato.minipro.service.mall;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.arvato.minipro.bean.bean_do.mall.CommodityCartDo;
import com.arvato.minipro.bean.bean_do.mall.EcShoppingCartDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_vo.mall.CommodityCartVo;
import com.arvato.minipro.bean.bean_vo.mall.EcCommodityVo;
import com.arvato.minipro.bean.bean_vo.mall.EcShoppingCartVo;
import com.arvato.minipro.mapper.mall.CommodityCartMapper;
import com.arvato.minipro.mapper.mall.EcShoppingCartMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * Describe：购物车相关service
 * 
 * @author smallking
 *
 *  2018年2月8日
 */
@Service
@Transactional
public class ShoppingCartService {
	
	@Autowired
	private EcShoppingCartMapper shoppingCartMapper;
	
	@Autowired
	private CommodityCartMapper commodityCartMapper;
	
	@Autowired
	private CommodityService commodityService;
	
	/**
	 * 根据Query条件查询购物车商品列表
	 * @param query
	 * @return
	 */
	public List<CommodityCartVo> listCommodityCartByQuery(Query query) {
		return commodityCartMapper.listCommodityCartByQuery(query);
	}
	/**
	 * 根据用户ID查询购物车商品列表
	 * @param query
	 * @return
	 */
	public PageData listCommodityCartByUserId(Query query) {
		// 开启分页 设置参数
		PageHelper.startPage(query.getPageNum(), query.getPageSize());
		// 开始查询
		List<CommodityCartVo> list = commodityCartMapper.listCommodityCartByUserId(query);
		// 得到分页结果
		PageInfo<CommodityCartVo> pageInfo = new PageInfo<CommodityCartVo>(list);
		List<CommodityCartVo> commodityCartList = pageInfo.getList();
		for(CommodityCartVo commodityCartVo : commodityCartList) {
			EcCommodityVo commodityVo = commodityService.getCommodityById(commodityCartVo.getCommodityId());
			commodityCartVo.setCommodityVo(commodityVo);
		}
		return new PageData(pageInfo.getTotal(), pageInfo.getList());
	}
	
	/**
	 * 将商品加入购物车或者修改购物车商品数量
	 * 
	 * @param commodityCartDo
	 * @return
	 */
	public int insert(CommodityCartDo commodityCartDo) {
		// 查询当前商品是否被当前用户加入到购物车过
		Query query = new Query();
		query.put("commodityId", commodityCartDo.getCommodityId());
		query.put("shoppingCartId", commodityCartDo.getShoppingCartId());
		List<CommodityCartVo> list = commodityCartMapper.listCommodityCartByQuery(query);
		// 如果当前购物车不存在此商品
		if(list == null || list.size() == 0) {
			// 而且当前是新增请求
			if(commodityCartDo.getCount() != -1) {
				return commodityCartMapper.insert(commodityCartDo);
			}
		}else {
			// 曾经被加入过 就更新
			CommodityCartVo commodityCartVo = list.get(0);
			System.out.println("数量：" + commodityCartVo.getCount());
			commodityCartDo.setCommodityCartId(commodityCartVo.getCommodityCartId());
			commodityCartDo.setCount(commodityCartVo.getCount() + commodityCartDo.getCount());
			if(commodityCartDo.getCount() <= 0) {
				// 执行删除
				return commodityCartMapper.deleteById(commodityCartDo.getCommodityCartId());
			}
			// 修改成功的话 将修改之后的count返回去
			int row = commodityCartMapper.update(commodityCartDo);
			if(row == 1) {
				int updatedCount = commodityCartDo.getCount();
				if(updatedCount <= 0) {
					updatedCount = 0;
				}
				return updatedCount;
			}
			return -500;
		}
		return 0;
	}
	/**
	 * 删除购物车记录
	 * @param query
	 */
	public int deleteByQuery(Query query) {
		return commodityCartMapper.deleteByQuery(query);
	}
	/**
	 * 根据用户ID查询购物车ID
	 * 如果没有 则新建
	 * @param userId
	 * @return
	 */
	public EcShoppingCartVo getShoppingCartByUserId(String userId) {
		EcShoppingCartVo cartVo = shoppingCartMapper.getShoppingCartByUserId(userId);
		if(cartVo == null) {
			EcShoppingCartDo cartDo = new EcShoppingCartDo();
			cartDo.setUserId(userId);
			cartDo.setStatus(1);
			try {
				int row = shoppingCartMapper.insert(cartDo);
				if(row == 1) {
					cartVo = new EcShoppingCartVo();
					cartVo.setShoppingCartId(cartDo.getShoppingCartId());
					return cartVo;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return cartVo;
	}
	

}
